﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace UtilLib
{
    /// <summary>
    /// 公共函数类  随机
    /// </summary>
    public static partial class Util
    {
        /// <summary>
        /// 排序 线性
        /// </summary>
        /// <param name="score">分数</param>
        /// <param name="rank">排名</param>
        /// <param name="rankStart">开始排名</param>
        /// <param name="rankEnd">最大排名</param>
        /// <param name="scoreStart">开始分数</param>
        /// <param name="scoreEnd">最小分数</param>
        /// <returns></returns>
        public static void CalculateLineRank(ref int rank, int score, int rankStart, int rankEnd, int scoreStart, int scoreEnd)
        {
            if (score <= scoreStart)
            {
                return;
            }
            if (rankEnd <= rankStart)
            {
                return;
            }
            if (scoreStart <= scoreEnd)
            {
                return;
            }
            int rankadd = 0;
            //排名差大于等于分差
            if ((rankEnd - rankStart) >= (scoreStart - scoreEnd))
            {
                rankadd = score - scoreStart;
            }
            else
            {
                float ratio = ((float)(scoreStart - scoreEnd)) / ((float)(rankEnd - rankStart));
                rankadd = (int)(((float)(score - scoreStart)) * ratio);
            }
            rank += rankadd;
        }
        
        /// <summary>
        /// 获取最近的一个大于值
        /// </summary>
        /// <param name="ll"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static int GetNearestEle(List<int> ll, int v)
        {
            int result = 0;
            foreach (var nn in ll)
            {
                if (nn > v)
                {
                    result = nn;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 移除 SortedDictionary 多余的元素 从头移除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="dict"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static void SortedDictionaryRemoveFirst<T1, T2>(ref SortedDictionary<T1, T2> dict, int count)
        {
            while (dict.Count > count)
            {
                dict.Remove(dict.First().Key);
            }
        }

    }
}